using System.Collections.Generic;
using BasicScript.Component;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class BodyDataItem : BaseItem
	{
		private enum State
		{
			Shrink = 0,
			Extend = 1
		}

		private enum Anim
		{
			Static = 0,
			Extend = 1,
			Shrink = 2
		}

		private const int MIN_NUM = 60;

		private const int MAX_NUM = 99;

		private const float SHRINK_1 = -45f;

		private const float SHRINK_2 = 45f;

		private const float EXTEND_1 = -60f;

		private const float EXTEND_2 = 100f;

		private State state;

		private Anim anim;

		private Transform pole1;

		private Transform pole2;

		private Transform point;

		private Transform displayer;

		private float animTimer;

		private float curAnimVal1;

		private float targetAnimVal1;

		private float curAnimVal2;

		private float targetAnimVal2;

		private Vector2 newPosition;

		private bool isShowNum;

		private int num1;

		private int num2;

		private int num3;

		private Transform num1Trans;

		private Transform num2Trans;

		private Transform num3Trans;

		private Dictionary<Transform, SpriteRenderer[]> numDic = new Dictionary<Transform, SpriteRenderer[]>();

		private float timer;

		private SpriteAssets spriteAssets;

		private FrameAnimation ecg;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			pole1 = model.Find("Pole1");
			pole2 = pole1.Find("Sprite/Pole2");
			pole2.SetLocalZ(-1E-05f);
			point = pole2.Find("Sprite/Point");
			displayer = model.Find("Displayer");
			displayer.SetLocalZ(-3E-05f);
			Transform trans = displayer.Find("Sprite");
			trans.SetLocalZ(1E-05f);
			spriteAssets = model.GetComponent<SpriteAssets>();
			num1Trans = displayer.Find("Num1");
			num2Trans = displayer.Find("Num2");
			num3Trans = displayer.Find("Num3");
			ecg = displayer.Find("ECG").GetComponent<FrameAnimation>();
			SetState((State)data.state);
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (isShowNum && state == State.Shrink)
			{
				timer += Time.deltaTime;
				if (timer >= 0.2f)
				{
					timer = 0f;
					num1 = Mathf.Clamp(num1 + Random.Range(-2, 3), 60, 99);
					num2 = Mathf.Clamp(num2 + Random.Range(-2, 3), 60, 99);
					num3 = Mathf.Clamp(num3 + Random.Range(-2, 3), 60, 99);
					SetNumber(num1, num1Trans);
					SetNumber(num2, num2Trans);
					SetNumber(num3, num3Trans);
				}
			}
			if (anim != 0)
			{
				animTimer += Time.deltaTime;
				pole1.SetLocalRotationZ(Mathf.Lerp(curAnimVal1, targetAnimVal1, animTimer));
				pole2.SetLocalRotationZ(Mathf.Lerp(curAnimVal2, targetAnimVal2, animTimer));
				newPosition = point.position + new Vector3(0.27f, -0.42f);
				displayer.SetXY(newPosition.x, newPosition.y);
				if (animTimer >= 1f)
				{
					anim = Anim.Static;
				}
			}
		}

		public void SetNumber(int num, Transform numTrans)
		{
			string[] array = new string[2];
			if (num < 10)
			{
				array[0] = "0";
				array[1] = num.ToString();
			}
			else
			{
				array[0] = num.ToString().Substring(0, 1);
				array[1] = num.ToString().Substring(1, 1);
			}
			SpriteRenderer[] array2 = new SpriteRenderer[2];
			if (numDic.ContainsKey(numTrans))
			{
				array2 = numDic[numTrans];
			}
			else
			{
				array2 = new SpriteRenderer[2]
				{
					numTrans.Find("0").GetComponent<SpriteRenderer>(),
					numTrans.Find("1").GetComponent<SpriteRenderer>()
				};
				numDic.Add(numTrans, array2);
			}
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i];
				array2[i].sprite = spriteAssets.GetSprite(text);
			}
		}

		private void CheckNumShow()
		{
			if (isShowNum && state == State.Shrink)
			{
				num1 = Random.Range(70, 89);
				num2 = Random.Range(70, 89);
				num3 = Random.Range(70, 89);
				ecg.Play();
				PlayAudio("sound_ecg");
			}
			else
			{
				num1 = 0;
				num2 = 0;
				num3 = 0;
				ecg.GotoFirstFrame();
				ecg.Stop();
				RemoveAudioPlayer();
			}
			SetNumber(num1, num1Trans);
			SetNumber(num2, num2Trans);
			SetNumber(num3, num3Trans);
			timer = 0f;
		}

		public void IsShowNum(bool val)
		{
			isShowNum = val;
			CheckNumShow();
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (anim == Anim.Static)
			{
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
				if (state == State.Shrink)
				{
					anim = Anim.Extend;
					state = State.Extend;
					curAnimVal1 = -45f;
					targetAnimVal1 = -60f;
					curAnimVal2 = 45f;
					targetAnimVal2 = 100f;
				}
				else if (state == State.Extend)
				{
					anim = Anim.Shrink;
					state = State.Shrink;
					curAnimVal1 = -60f;
					targetAnimVal1 = -45f;
					curAnimVal2 = 100f;
					targetAnimVal2 = 45f;
				}
				CheckNumShow();
				animTimer = 0f;
			}
		}

		private void SetState(State val)
		{
			state = val;
			if (state == State.Extend)
			{
				pole1.SetLocalRotationZ(-60f);
				pole2.SetLocalRotationZ(100f);
				coll.offset = new Vector2(0.9f, -0.48f);
			}
			else if (state == State.Shrink)
			{
				pole1.SetLocalRotationZ(-45f);
				pole2.SetLocalRotationZ(45f);
				coll.offset = new Vector2(1.45f, -0.52f);
			}
			newPosition = point.position + new Vector3(0.27f, -0.42f);
			displayer.SetXY(newPosition.x, newPosition.y);
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = (int)state;
		}
	}
}
